package editeurimpl;

import Nuplet.Pair;

/**
 * @(#) Buffer.java
 */

public class Buffer
{
///la chaine buffer
        private StringBuffer buffer ;
        
        public Buffer(){buffer = new StringBuffer();}
        public int length(){return buffer.length();}
        
        public void inserer( int pos,String t ){buffer.insert(pos, t);}
        public void inserer( int pos,char c ){buffer.insert(pos, c);}
        public void inserer( String t , int p){buffer.insert(p, t);}
        
        public void delchar(int pos){buffer.deleteCharAt(pos);}


        public void effacer( int deb, int fin ){buffer.delete(deb,fin);}
        public String toString(){
                return buffer.toString();
        }
        public void reset()
        {buffer=new StringBuffer();}
        public String gettext( int deb, int fin )
        {return buffer.substring(deb,fin);}
        public int IndexToLineNumber(int index){
                int numligne=1;
                for (int i=0;i<index;i++){
                        if (buffer.charAt(i)=='\n'){
                                numligne++;
                        }
                }
                
                return numligne;
        }
        public Pair<Integer,Integer> IndexToCoord(int index){
            int numligne=1;
            int numcol=1;
            for (int i=0;i<index;i++){
            	 	numcol++;
                    if (buffer.charAt(i)=='\n'){
                            numligne++;
                            numcol=1;
                    }
                   
            }
            
            return new Pair<Integer,Integer>(numligne,numcol);
        }
        
        public int DebutDeLigne(int index){
                int debut=0;
                for (int i=0;i<index;i++){
                        if (buffer.charAt(i)=='\n'){
                                debut=i+1;
                        }
                }
                
                return debut;
        }
        public int FinDeLigne(int index){
                int i;boolean notfound=true;
                for (i=index;i<buffer.length() && notfound ;i++){
                        notfound=buffer.charAt(i)!='\n';
                }
                return i;
        }
        public int LineNumberToIndex(int pos) {
                int index;
                int numligne=1;
                for (index=0;numligne<pos && index<buffer.length();index++){
                        if (buffer.charAt(index)=='\n'){
                                numligne++;
                        }
                }
                
                return index;
        }

                
}